package com.foodchain.service;

import com.foodchain.consts.Consts;
import com.foodchain.entity.ActivityOuts;
import com.foodchain.entity.ActivitySchedule;
import com.foodchain.entity.News;
import com.foodchain.repository.ActivityOutsRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.List;

/**
 * @ClassName ActivityOutsService
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/19 15:23
 * @Version 1.0
 **/
@Service
public class ActivityOutsService {
    @Resource
    private ActivityOutsRepository activityOutsRepository;


    public Page<ActivityOuts> findAll(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        Specification<ActivityOuts> spec = new Specification<ActivityOuts>() {        //查询条件构造
            public Predicate toPredicate(Root<ActivityOuts> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Boolean> status= root.get("outsStatus");
                Predicate p1 = cb.isTrue(status);
                Predicate p = cb.and(p1);
                return p;
            }
        };

        Page<ActivityOuts> all = activityOutsRepository.findAll(spec, pageable);

        if (all.getContent().size() > 0) {
            for (ActivityOuts outs : all) {
                outs.setOutsImg(Consts.FileUrl.file_url+"/"+ outs.getOutsImg());
            }
        }
        return all;
    }
}
